package com.yunhe.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import com.yunhe.biz.ISubjectBiz;

import com.yunhe.biz.impl.SubjectBizImpl;

import com.yunhe.entity.Page;
import com.yunhe.entity.Subject;
import com.yunhe.entity.User;
import com.yunhe.util.PageUtil;

/**
 * 列表展示action
 * */
public class ListAction extends ActionSupport{
	//创建user对象
	private User user;
	//创建json对象
	private JSONObject obj;
	//页码
	private Integer page;
	//大小
	private Integer rows;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public JSONObject getObj() {
		return obj;
	}
	public void setObj(JSONObject obj) {
		this.obj = obj;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	//ISubjectBiz sb = new SubjectBizImpl();
	private ISubjectBiz sb;
	
	public ISubjectBiz getSb() {
		return sb;
	}
	public void setSb(ISubjectBiz sb) {
		this.sb = sb;
	}
	public String execute(){
		
		Integer tal = sb.searchSubjectCount();
		if(rows==null){
			rows = 3;
		}
		if(page==null){
			page = 1;
		}
		//创建分页对象
		Page pages = PageUtil.createPage(rows, tal, page);	
		//查看每页显示个数
		Subject sub = new Subject();
		List<Subject> list = sb.searchSubjects(sub, pages);
		
		Map map = new HashMap();
		map.put("total", tal);
		
		List lsit = new ArrayList();
		
		for (Subject subject : list) {
			Map itemMap = new HashMap();
			itemMap.put("id", subject.getVs_id());
			itemMap.put("title", subject.getVs_title());
			
			itemMap.put("options", subject.getOptions().size());
			itemMap.put("entries", subject.getItems().size());
			
			lsit.add(itemMap);
		}
		map.put("rows", lsit);
		
		this.setObj(JSONObject.fromObject(map));
		
		return SUCCESS;
	}
	
}
